/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.prototype.vaadin;

import com.vaadin.Application;
import com.vaadin.terminal.ClassResource;
import com.vaadin.terminal.Resource;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Label;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.MenuBar.MenuItem;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import java.io.File;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mdcolin
 */
public class index extends Application{
    String app_path;
    
    @Override
    public void init() {
        final Window mainWindow = new Window("Recover SF Code");
        try {
            app_path="C:\\Users\\Cmd\\Desktop\\M1 MIAGE\\CAR\\Projet\\";
            
            // Recovering properties file
            AppProperties prop = new AppProperties(app_path+ File.separator +"recover_sf_code"+ File.separator + "web" + File.separator + "WEB-INF"+ File.separator +"french_label.properties");
        
            // Create the subwindow "About us"
            final Window subwindow = new Window(prop.getPropValue("item_about_us"));
            subwindow.setCaption(prop.getPropValue("item_about_us"));
            subwindow.setModal(true);
            subwindow.setWidth(200, 1);
            subwindow.setResizable(false);

            // Define a command for "About us" item
            MenuBar.Command cmd_about_us = new MenuBar.Command() {
                @Override
                public void menuSelected(MenuItem selectedItem) {
                    if (subwindow.getParent() != null) {
                        // window is already showing
                        mainWindow.showNotification("Window is already open");
                    } else {
                        // Open the subwindow by adding it to the parent
                        // window
                        mainWindow.addWindow(subwindow);
                    }
                }
            };
            
            // Create a Menubar
            final MenuBar menubar = new MenuBar();
            menubar.setSizeFull();
            MenuBar.MenuItem home=  menubar.addItem(prop.getPropValue("item_home"), null, null);
            MenuBar.MenuItem question =  menubar.addItem("?", null, null);
            MenuBar.MenuItem about_us = question.addItem(prop.getPropValue("item_about_us"), null, cmd_about_us);
            MenuBar.MenuItem help = question.addItem(prop.getPropValue("item_help"), null, null);
            
            // Subwindows data
            Label lbl_authors = new Label("<center>This application was developed by : <br/>"
                    + "BOUKHEZAR Zineddine<br/>"
                    + "COLIN MD<br/>"
                    + "MAIGA Fatoumata<br/>"
                    + "MOUNISSAMY Nadia</center>");
            lbl_authors.setSizeFull();
                    //setHeight(100, 1);
            //lbl_authors.setWidth(100, 1);
            lbl_authors.setContentMode(Label.CONTENT_XHTML);
            
            // Page title
            Label lbl_title = new Label("<h1><center>Recover SourceForge Code</center></h1>");
            lbl_title.setHeight(50, 1);
            lbl_title.setContentMode(Label.CONTENT_XHTML);
                        
            subwindow.addComponent(lbl_authors);
            mainWindow.addComponent(menubar);
            mainWindow.addComponent(lbl_title);  
            
            tableSheet(mainWindow, prop);

        } catch (Exception ex) {
            System.out.println("Error page index : "+ ex);
        }
        setMainWindow(mainWindow);
    }
    
    public void tableSheet(Window mainWindow, AppProperties prop) throws Exception{
        TabSheet tabS_Main;

        // Tab 1 content
        VerticalLayout tab_home = new VerticalLayout();
        tab_home.setMargin(true);
        Resource dd = new ClassResource("image/sourceforge.png", this);
        Embedded embdd_sf_picture = new Embedded("SourceForge Logo", dd);
        tab_home.addComponent(embdd_sf_picture);
        
        // Tab 2 content
        VerticalLayout tab_search = new VerticalLayout();
        tab_search.setMargin(true);
        SearchInterface search_interface = new SearchInterface();
        search_interface.setApp_path(app_path);
 
        try {
            search_interface.RecoverForm(tab_search);
        } catch (Exception ex) {
            Logger.getLogger(index.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        // Tab 3 content
        VerticalLayout tab_result = new VerticalLayout();
        tab_result.setMargin(true);
        // Create the table with a caption
        Table table_result = new Table(prop.getPropValue("table_result_caption"));
        table_result.setSizeFull();

        // Define the names and data types of columns
        // The "default value" parameter is meaningless here
        table_result.addContainerProperty(prop.getPropValue("table_result_col1"), String.class,  null);
        table_result.addContainerProperty(prop.getPropValue("table_result_col2"),  String.class,  null);
        table_result.addContainerProperty(prop.getPropValue("table_result_col3"),  String.class, null);

        // Add a few items in the table
//        Test test= new Test("http://sourceforge.net/directory/natlanguage:french/language:php/freshness:recently-updated/");
//        
//        //test.recupèreLesProjets(urlPrincipale);
//
//        for(int i=0;i<test.getMalist_de_lien().size();i++){
//                table_result.addItem(
//                        new Object[]{test.getMalist_de_lien().get(i),
//                            "", ""}, new Integer(i));
//                //System.out.println(test.getMalist_de_lien().get(i));
//        }
//        for(int i=0;i<test.getMalist_deText_Delien().size();i++){
//                System.out.println("nom: "+test.getMalist_deText_Delien().get(i));
//        }
//        /*for (Element link : links) {
//                String linkText = link.text();
//                System.out.println(malist_deText_Delien.size()+"  eeeee"+ linkText);
//        }*/
//            
//        tab_result.addComponent(table_result);
//        tab_result.addComponent(new Label("Nombre de pages : "+test.nbPage()));
//        tab_result.addComponent(new Label(test.urlPage().toString()));
        
        // code  de newmain
        PremiereConnection test;
        System.out.println("zino");
        TextField exppath = new TextField("Chemin :", "http://sourceforge.net/directory/os:windows/license:publicdomain/");
        exppath.setWidth("500px");
        test = new PremiereConnection(exppath.toString()); 
       
        int numWorkers = test.nbPage();
        numWorkers--;    
       
        ExecutorService pooldynamique = Executors.newCachedThreadPool();
        
            
        JobThread workers[] = new JobThread[numWorkers];
        Future<ConcurrentHashMap<String,String>> futures[] = new Future[numWorkers];
        
        for(int i=0;i<numWorkers;i++){
            //Thread.sleep(2000);

            workers[i] = new JobThread(test.urlPage().get(i));
            futures[i]= pooldynamique.submit(workers[i]);
        }
             
        pooldynamique.shutdown();
           
        for (int i = 0; i < numWorkers; i++) {
            try {    
                test.concurrentMap.putAll((futures[i].get()));
            } catch (Exception e) {}
        }     
        int i=0;                
        for(ConcurrentHashMap.Entry<String,String> entry : test.concurrentMap.entrySet()) {
            String cle = entry.getKey();
            String valeur = entry.getValue();
            table_result.addItem(
                        new Object[]{cle, valeur, i}, new Integer(i));
            System.out.println(i+" Nom: "+cle +" Lien: "+valeur);
//            CmdLineRecover c = new CmdLineRecover();
//            c.getCodeURL("http://sourceforge.net"+valeur);
            i++;
        }
        
        tab_result.addComponent(exppath);
        tab_result.addComponent(table_result);
         
        // Add the tab in the tabSheet
        tabS_Main = new TabSheet();
        tabS_Main.setSizeFull();

        tabS_Main.addTab(tab_home, prop.getPropValue("item_home"), null);
        tabS_Main.addTab(tab_search, prop.getPropValue("subwindow_search"), null);
        tabS_Main.addTab(tab_result, prop.getPropValue("subwindow_result"), null);
        //tabS_Main.addListener(this);

        mainWindow.addComponent(tabS_Main);
    }
    
}
